package cc.forgemod.customicon.client;

import com.google.common.collect.ImmutableList;

import cc.forgemod.customicon.client.texture.CResourceLocation;
import cc.forgemod.customicon.client.texture.CTextureGetter;
import cc.forgemod.customicon.client.texture.GifItemOverrideList;
import cc.forgemod.customicon.client.texture.TextureCIItem;
import cc.forgemod.customicon.client.texture.TextureGifCIItem;
import cc.forgemod.customicon.client.texture.model.BakedModelImpl;
import cc.forgemod.customicon.common.icon.Icon;
import cc.forgemod.customicon.common.icon.IconKey;
import cc.forgemod.customicon.common.icon.IconManager;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.item.ItemStack;
import net.minecraft.util.registry.IRegistry;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.model.ItemLayerModel;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ResourceListener implements ItemMeshDefinition {

    public static final ResourceListener INSTANCE = new ResourceListener();
    /**
     * 由{@link #onBakeModel(ModelBakeEvent)} 方法设置值,默认为null
     */
    protected static IRegistry<ModelResourceLocation, IBakedModel> mModelRegistry = null;

    @Override
    public ModelResourceLocation getModelLocation(ItemStack pStack) {
        IconKey tKey = IconKey.c(pStack);
        Icon tIcon = IconManager.getIcon(tKey);
        if (tIcon != null && tIcon.initSuccess()) {
            return new ModelResourceLocation(tKey.toString());
        }

        return null;
    }

    //----Plan B start

    @SubscribeEvent
    public void onBakeModel(ModelBakeEvent pEvent) {
        ResourceListener.mModelRegistry = pEvent.getModelRegistry();
        for (Icon sIcon : IconManager.getAllIcons().values()) {
            if (sIcon.initSuccess()) ResourceListener.registerBakeModel(sIcon.getKey(), sIcon.getTexture().getBakedModel());;
        }
    }

    public static void registerBakeModel(IconKey pKey, IBakedModel pModel) {
        if (ResourceListener.mModelRegistry == null) return;

        ResourceListener.mModelRegistry.putObject(new ModelResourceLocation(pKey.toString()), pModel);
    }

    //----Plan B end    

    /**
     * 根据提供的Meta值,将指定的材质转换为IBakedModel
     * 
     * @param pMeta
     *            物品Meta值,与Icon对应
     * @param pUseOvderrideBake
     *            是否启用IBakedModel的Override
     * @return
     */
    public static IBakedModel bakeTexture(TextureCIItem pTexture) {
        return bakeTexture(pTexture, pTexture instanceof TextureGifCIItem);
    }

    /**
     * 根据提供的Meta值,将指定的材质转换为IBakedModel
     * 
     * @param pMeta
     *            物品Meta值,与Icon对应
     * @param pUseOvderrideBake
     *            是否启用IBakedModel的Override
     * @return
     */
    public static IBakedModel bakeTexture(TextureCIItem pTexture, boolean pUseOvderrideBake) {
        ItemOverrideList tOver = ItemOverrideList.NONE;
        final Icon tIcon = pTexture.getIcon();
        if (pTexture instanceof TextureGifCIItem && pUseOvderrideBake) {
            tOver = new GifItemOverrideList((TextureGifCIItem)pTexture);
        }

        ItemLayerModel tLayerModel = new ItemLayerModel(ImmutableList.of(new CResourceLocation(pTexture.getIcon().getKey())), tOver);
        final IBakedModel tProxy = tLayerModel.bake(tLayerModel.getDefaultState(), DefaultVertexFormats.ITEM, CTextureGetter.INSTANCE);

        return new BakedModelImpl(tProxy, tIcon);
    }

}
